﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bigger.Core.Mvc;

namespace Bigger.Study.ConsoleApp
{
    public class BiggerCoreMvcExample : IExample
    {
        public void Execute()
        {
            ProcessConfiguration.Filters.Add(new MethodFilterDemo1Attribute());

            ProcessHandlerDemo objProcessHandlerDemo = new ProcessHandlerDemo();
            //objProcessHandlerDemo.ExecuteProcess("Test1", null);
            //objProcessHandlerDemo.ExecuteProcess("Test1", new { name = "Fish1" });
            //objProcessHandlerDemo.ExecuteProcess("Test1", new { name1 = "Fish1", name2 = "Fish2" });
            objProcessHandlerDemo.ExecuteProcess("Test1", new { s1 = "Fish1", i1 = 10 });
            //objProcessHandlerDemo.ExecuteProcess("Test2", null);
            objProcessHandlerDemo.ExecuteProcess("Test2", new { name = "Fish1" });
            //objProcessHandlerDemo.ExecuteProcess("Test2", new { name1 = "Fish1", name2 = "Fish2" });
            objProcessHandlerDemo.ExecuteProcess("Test3", null);
            //objProcessHandlerDemo.ExecuteProcess("Test3", new { name = "Fish1" });
            //objProcessHandlerDemo.ExecuteProcess("Test3", new { name1 = "Fish1", name2 = "Fish2" });
        }
    }

    [MethodFilterDemo1]
    public class ProcessHandlerDemo : ProcessHandler
    {
        public override void OnMethodExecuting(MethodExecutingContext filterContext)
        {
            Console.WriteLine("ProcessHandlerDemo.OnMethodExecuting：" + this.Scope);
        }
        public override void OnMethodExecuted(MethodExecutedContext filterContext)
        {
            Console.WriteLine("ProcessHandlerDemo.OnMethodExecuted：" + this.Scope);
        }
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            Console.WriteLine("ProcessHandlerDemo.OnResultExecuting");
        }
        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            Console.WriteLine("ProcessHandlerDemo.OnResultExecuted");
        }
        public override void OnException(ExceptionFilterContext filterContext)
        {
            Console.WriteLine("ProcessHandlerDemo.OnException");
            filterContext.ExceptionHandled = true;
        }

        //public ProcessResult Test1()
        //{
        //    Console.WriteLine("ProcessHandlerDemo.Test1");
        //    return Void();
        //}
        //[MethodFilterDemo1]
        //[MethodFilterDemo2]
        ////[MethodFilterDemo2(Order = -1)]
        //public ProcessResult Test1(string name)
        //{
        //    Console.WriteLine("ProcessHandlerDemo.Test1(name)");
        //    return Void();
        //}
        //[MethodFilterDemo1]
        //[MethodFilterDemo2]
        //[OverrideFilter(typeof(MethodFilterDemo1Attribute))]
        //public ProcessResult Test1(string name1, string name2 = "WWW")
        //{
        //    Console.WriteLine("ProcessHandlerDemo.Test1(name1,name2)");
        //    return Void();
        //}
        [MethodFilterDemo1]
        [MethodFilterDemo2]
        //[OverrideFilter(typeof(MethodFilterDemo1Attribute))]
        public ProcessResult Test1(string s1, int? i1 = 20)
        {
            Console.WriteLine("ProcessHandlerDemo.Test1(s1,i1)");
            return Void();
        }
        //public ProcessResult Test2()
        //{
        //    return new ProcessDemoResult();
        //}
        [ResultFilterDemo]
        public ProcessResult Test2(string name)
        {
            return new ProcessDemoResult();
        }
        //[ResultFilterDemo]
        //[OverrideFilter(typeof(ResultFilterDemoAttribute))]
        //public ProcessResult Test2(string name1, string name2)
        //{
        //    return new ProcessDemoResult();
        //}
        public ProcessResult Test3()
        {
            throw new Exception();
            return Void();
        }
        //[ExceptionFilterDemo]
        //public ProcessResult Test3(string name)
        //{
        //    throw new Exception();
        //    return Void();
        //}
        //[ExceptionFilterDemo]
        //[OverrideFilter(typeof(ExceptionFilterDemoAttribute))]
        //public ProcessResult Test3(string name1, string name2)
        //{
        //    throw new Exception();
        //    return Void();
        //}
    }

    public class ProcessDemoResult : ProcessResult
    {
        public override void ExecuteResult(ResultContext objResultContext)
        {
            Console.WriteLine("ProcessDemoResult.ExecuteResult");
        }
    }

    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
    public class MethodFilterDemo1Attribute : MethodFilterAttribute
    {
        public override void OnMethodExecuting(MethodExecutingContext filterContext)
        {
            Console.WriteLine("MethodFilterDemo1Attribute.OnMethodExecuting："+this.Scope);
        }

        public override void OnMethodExecuted(MethodExecutedContext filterContext)
        {
            Console.WriteLine("MethodFilterDemo1Attribute.OnMethodExecuted：" + this.Scope);
        }
    }

    public class MethodFilterDemo2Attribute : MethodFilterAttribute
    {
        public override void OnMethodExecuting(MethodExecutingContext filterContext)
        {
            Console.WriteLine("MethodFilterDemo2Attribute.OnMethodExecuting");
        }

        public override void OnMethodExecuted(MethodExecutedContext filterContext)
        {
            Console.WriteLine("MethodFilterDemo2Attribute.OnMethodExecuted");
        }
    }

    public class ResultFilterDemoAttribute : ResultFilterAttribute
    {
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            Console.WriteLine("ResultFilterDemoAttribute.OnResultExecuting");
        }
        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            Console.WriteLine("ResultFilterDemoAttribute.OnResultExecuted");
        }
    }

    public class ExceptionFilterDemoAttribute : ExceptionFilterAttribute
    {
        public override void OnException(ExceptionFilterContext filterContext)
        {
            Console.WriteLine("ExceptionFilterDemoAttribute.OnException");
            filterContext.ExceptionHandled = true;
            filterContext.Result = new ProcessDemoResult();
        }
    }
}
